package net.dgg.zz.credential.storageCapacity.controller;

import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.iboss.base.util.datatable.DataTableUtils;
import net.dgg.iboss.base.util.datatable.domain.DataTableRequest;
import net.dgg.tmd.foundation.platform.org.service.OrganizationManager;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.zz.common.constant.CredentialConstant;
import net.dgg.zz.common.service.CommonService;
import net.dgg.zz.common.utils.DataTableResponse;
import net.dgg.zz.credential.storageCapacity.entity.StorageCapacityVo;
import net.dgg.zz.credential.storageCapacity.service.StorageCapacityService;
import net.dgg.zz.credential.warningConfigure.service.SystemConfigureService;
import net.fblock.log.NLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/zs/credential/stotage_capacity")
public class StorageCapacityController extends DggBaseController {

    @Autowired
    private SessionManager sessionManager;
    @Autowired
    private OrganizationManager organizationManager;
    @Autowired
    private StorageCapacityService storageCapacityService;
    @Autowired
    private SystemConfigureService systemConfigureService;
    @Autowired
    private CommonService commonService;

    /**
     * 总览的库容预警显示
     *
     * @param params
     * @param request
     * @return
     */
    @NLog
    @RequestMapping("/get_stotage_capacity")
    @ResponseBody
    public DataTableResponse queryDataWithPage(@RequestParam Map params, HttpServletRequest request) {
        try {
            DataTableRequest r = DataTableUtils.getParam(request);
            params.put("page", r.getPage());//获取当前页
            params.put("limit", r.getLength());//获取每页条数
            params.put("generalType", CredentialConstant.ZZ_CREDENTIAL);
            List<StorageCapacityVo> storageCapacities = storageCapacityService.queryStorageCapacityWithPage(params);
            return new DataTableResponse().data(params, storageCapacities);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataTableResponse().data(params, null);
        }
    }

    /**
     * 普通证书库容预警
     *
     * @param model
     * @return
     */
    @NLog()
    @RequestMapping("/warning_page")
    public String warningPage(Model model) {
        // 适用范围
        List<Map> applicationScopes = commonService.queryTreeBook(CredentialConstant.CREDENTIAL_APPLICATION_SCOPE, 1, 2, null);
        model.addAttribute("applicationScopes", applicationScopes);
        model.addAttribute("generalType", CredentialConstant.GENERAL_CREDENTIAL);
        return "/zzsc/credential/generalCredential/warning_page";
    }

    /**
     * 普通证书库容预警
     *
     * @param model
     * @return
     */
    @NLog()
    @RequestMapping("/warning_data")
    @ResponseBody
    public DataTableResponse warningData(@RequestParam Map params, HttpServletRequest request) {
        try {
            DataTableRequest r = DataTableUtils.getParam(request);
            params.put("page", r.getPage());//获取当前页
            params.put("limit", r.getLength());//获取每页条数
            params.put("generalType", CredentialConstant.GENERAL_CREDENTIAL);
            List<StorageCapacityVo> storageCapacities = storageCapacityService.queryStorageCapacityWithPage(params);
            return new DataTableResponse().data(params, storageCapacities);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataTableResponse().data(params, null);
        }

    }

    /**
     * 设置库容预警
     *
     * @param model
     * @return
     */
    @NLog()
    @RequestMapping("/set_warning")
    @ResponseBody
    public String setWarning(Model model) {
        // 适用范围
        List<Map> applicationScopes = commonService.queryTreeBook(CredentialConstant.CREDENTIAL_APPLICATION_SCOPE, 1, 2, null);
        model.addAttribute("applicationScopes", applicationScopes);
        return "/zzsc/credential/generalCredential/general_certificate_info";
    }


    /**
     * 普通证书库容预警(根据大类型统计)
     *
     * @param model
     * @return
     */
    @NLog()
    @RequestMapping("/warning_page_by_type")
    public String warningPageV2(Model model) {
        // 适用范围
        List<Map> applicationScopes = commonService.queryTreeBook(CredentialConstant.CREDENTIAL_APPLICATION_SCOPE, 1, 2, null);
        model.addAttribute("applicationScopes", applicationScopes);
        model.addAttribute("generalType", CredentialConstant.GENERAL_CREDENTIAL);
        return "/zzsc/credential/generalCredential/warning_page_by_type";
    }


    /**
     * 普通证书库容总览（根据大类型统计）
     *
     * @param model
     * @return
     */
    @NLog()
    @RequestMapping("/warning_data_by_type")
    @ResponseBody
    public DataTableResponse warningDataV2(@RequestParam Map params, HttpServletRequest request) {
        try {
            DataTableRequest r = DataTableUtils.getParam(request);
            params.put("page", r.getPage());//获取当前页
            params.put("limit", r.getLength());//获取每页条数
            params.put("generalType", CredentialConstant.GENERAL_CREDENTIAL);
            List<Map> storageCapacities = storageCapacityService.queryStorageCapacityByTypeWithPage(params);
            return new DataTableResponse().data(params, storageCapacities);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataTableResponse().data(params, null);
        }

    }
}
